
********************************************************************************
*This file analyzes Bank LPI
********************************************************************************

cd ".."

use "data_input/bankassetsliab.dta", clear


merge 1:1 rssd year quarter using "data_intermediate/bank_ltisample_haircutrobust_new.dta"
keep if _merge==3
drop _merge 


gen insuredratio=depins/deposits
replace insuredratio=1 if insuredratio>1

gen lnassets=log(assets)

gen runprob=0
replace runprob=100 if lpi==0

gen nodepliab=(assets-deposits)/assets

gen cashratio=(a_b_treas+a_b_ag_deb+a_b_cash+a_b_ag_mbscmo)*100/assetstotal

gen assetilliquidity=(a_b_cons_loan*awgt_cons_loan+ a_b_com_loan*awgt_com_loan + awgt_cmoabs*a_b_cmoabs+ awgt_bond*a_b_bond ///
+awgt_mm*a_b_mm+awgt_muni*a_b_muni+ awgt_o_realestate*a_b_o_realestate+ awgt_sf_realestate*a_b_sf_realestate+ awgt_eq*a_b_eq ///
+ awgt_fasset*a_b_fasset) 

replace assetilliquidity=1-assetilliquidity/(a_b_cons_loan+ a_b_com_loan+a_b_cmoabs+a_b_bond+a_b_mm+ a_b_o_realestate+ a_b_muni+ a_b_sf_realestate+a_b_eq + a_b_fasset)

gen cash_weight=a_b_cash*100/assetstotal

gen treas_weight=a_b_treas*100/assetstotal

gen ag_deb_weight=a_b_ag_deb*100/assetstotal

gen ag_mbscmo_weight=a_b_ag_mbscmo*100/assetstotal

gen mm_weight=a_b_mm*100/assetstotal

gen muni_weight=a_b_muni*100/assetstotal

gen bond_weight=a_b_bond*100/assetstotal

gen eq_weight=a_b_eq*100/assetstotal

gen cmoabs_weight=a_b_cmoabs*100/assetstotal

gen com_loan_weight=a_b_com_loan*100/assetstotal

gen con_loan_weight=a_b_cons_loan*100/assetstotal

gen s_realestate_weight=a_b_sf_realestate*100/assetstotal

gen o_realestate_weight=a_b_o_realestate*100/assetstotal

gen fasset_weight=a_b_fasset*100/assetstotal


gen netincratio=(netinc+nonintexp)/assets
winsor2 netincratio, cuts(1 99) replace
gen nlpi=lpi-netincratio/liq_value_pct


collapse (mean) insuredratio runprob lpi lpi_lag nlpi  lnassets nodepliab ///
cashratio assetilliquidity cash_weight treas_weight ag_deb_weight ag_mbscmo_weight mm_weight ///
fasset_weight muni_weight bond_weight cmoabs_weight eq_weight com_loan_weight con_loan_weight ///
s_realestate_weight o_realestate_weight, by (rssd)


foreach x in lpi lpi_lag nlpi {
replace `x'=. if `x'>1 & `x'!=. 
replace `x'=`x'*10000
}

egen loanweight=rowtotal(com_loan_weight  con_loan_weight s_realestate_weight o_realestate_weight)

foreach x in nodepliab  insuredratio {
winsor2 `x', cuts(1 99) replace 
}

foreach x in  cashratio assetilliquidity lnassets {
winsor2 `x', cuts(1 99) replace 
rename `x' temp 
egen `x'=std(temp)
drop temp
}

foreach x in runprob {
rename `x' temp 
egen `x'=std(temp)
drop temp
}


label var lpi "LPI"
label var lpi_lag "LPI"

label var insuredratio "Insured Dep. Ratio"
label var nodepliab  "Non-deposits Liab."
label var cashratio  "Cash Ratio"
label var lnassets  "Asset Size"
label var assetilliquidity "Asset Illiquidity"

label var cash_weight "Cash"
label var treas_weight "Treasuries"
label var ag_deb_weight "Agency Debentures"
label var ag_mbscmo_weight "Agency MBS/CMO"
label var mm_weight "Money Market"
label var muni_weight "Muni"
label var bond_weight "Corporate Bonds"
label var eq_weight "Equities"
label var cmoabs_weight "CMO/ABS"
label var com_loan_weight "Commercial Loans"
label var  con_loan_weight "Consumer Loans"
label var s_realestate_weight "Real Estate (Family Housing)"
label var o_realestate_weight "Real Estate (Other)"
label var runprob  "Default Prob"
label var loanweight "Loans" 
label var fasset_weight "Fixed Assets" 


********************************************************************************
*Cross-sectional determinants of Bank LPI
********************************************************************************

eststo clear
eststo: reg lpi insuredratio 
eststo: reg lpi insuredratio  nodepliab 
eststo: reg  lpi insuredratio nodepliab assetilliquidity 
eststo: reg  lpi insuredratio nodepliab runprob assetilliquidity cashratio  lnassets
capture: esttab using  "output/bankcross.tex", ///
replace b(2) se(2) star(* 0.10 ** 0.05 *** 0.01  )  ///
mgroups("LPI", pattern( 1 0 0 0 ) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
ar2   label  nomtitles nodepvars nonotes  s(N r2_a, ///
label( "Observations" "Adjusted R2") ///
fmt( %9.0fc 2 )) 
 
********************************************************************************
*Summmary Statistics Table Output
********************************************************************************

eststo clear
estpost summarize cash_weight treas_weight ag_deb_weight ag_mbscmo_weight mm_weight muni_weight bond_weight cmoabs_weight eq_weight  loanweight fasset_weight  , detail 
esttab  using "output/bankstats.tex", replace cells("mean(fmt(2)) sd(fmt()) p25(fmt(2))  p50(fmt(2))  p75(fmt(2)) ") nomtitle   width(0.8\hsize) label  nonumber noobs


eststo clear
estpost summarize lpi, detail 
esttab  using "output/banklpistats.tex", replace cells("mean(fmt(1)) sd(fmt(1)) p25(fmt(1))  p50(fmt(1))  p75(fmt(1)) ") nomtitle   width(0.8\hsize) label  nonumber noobs

eststo clear
estpost summarize lpi_lag, detail 
esttab  using "output/banklpistats_lag.tex", replace cells("mean(fmt(1)) sd(fmt(1)) p25(fmt(1))  p50(fmt(1))  p75(fmt(1)) ") nomtitle   width(0.8\hsize) label  nonumber noobs


eststo clear
estpost summarize nlpi, detail 
esttab  using "output/banklpistats_netfees.tex", replace cells("mean(fmt(1)) sd(fmt(1)) p25(fmt(1))  p50(fmt(3))  p75(fmt(1)) ") nomtitle   width(0.8\hsize) label  nonumber noobs


***********************************************************************************
*Robustness Table: Loan liquidation costs are X times HY
************************************************************************************

use "data_intermediate/bank_ltisample_haircutrobust_1.dta", clear

rename lpi lpi_hy1

merge 1:1 rssd tq using "data_intermediate/bank_ltisample_haircutrobust_2.dta"
drop _merge

rename lpi lpi_hy2

merge 1:1 rssd tq using "data_intermediate/bank_ltisample_haircutrobust_3.dta"
drop _merge

rename lpi lpi_hy3

merge 1:1 rssd tq using "data_intermediate/bank_ltisample_haircutrobust_4.dta"
drop _merge

rename lpi lpi_hy4

replace lpi_hy1=lpi_hy1*10000
replace lpi_hy2=lpi_hy2*10000
replace lpi_hy3=lpi_hy3*10000
replace lpi_hy4=lpi_hy4*10000


collapse  (mean)  lpi_hy1 lpi_hy2 lpi_hy3 lpi_hy4, by (rssd)

label var lpi_hy1 "LPI (HY1)"
label var lpi_hy2 "LPI (HY2)"
label var lpi_hy3 "LPI (HY3)"
label var lpi_hy4 "LPI (HY4)"

eststo clear
estpost summarize lpi_hy1 lpi_hy2 lpi_hy3 lpi_hy4, detail 
esttab  using "output/banklpistats_hy2.tex", replace cells("mean(fmt(1)) sd(fmt(1)) p25(fmt(1))  p50(fmt(1))  p75(fmt(1)) ") nomtitle   width(0.8\hsize) label  nonumber noobs
